package org.liveboardgames.agricola.vo.card
{
	import com.adobe.cairngorm.vo.IValueObject;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	
	import org.liveboardgames.agricola.view.component.ImageHandler;
	import org.liveboardgames.agricola.vo.common.Peep;
	import org.liveboardgames.agricola.vo.common.Target;
	import org.liveboardgames.common.dictionary.collections.HashCollection;

	[RemoteClass(alias="org.liveboardgames.agricola.domain.card.Card")]
	[Bindable]
	public class Card implements Target, IValueObject
	{
		/**
		 * Each card has a name, that is localized depending
		 * on the language.
		 */
		public var cardName:String;
		
		public var changingType:String;
		public var period:int;

		/**
		 * Unique identifier of the card
		 */
		public var cardId:int;

		/**
		 * The list of actions granted by the card, i.e. its effects.
		 */
		public var actions:ListCollectionView;

		public var exclusiveActions:ListCollectionView;

		/**
		 * The resources that are present on the card itself.
		 * The resources map is really a Map<ResourceType, Integer>
		 */
		private var _resources:HashCollection;

		/**
		 * The list of peeps that are on the card. It is really a
		 * List<PeepContainer> so that a peep can be linked to its
		 * owner.
		 */
		public var peeps:ListCollectionView;

		/**
		 * The graphical image attached to the card. Default is a transparent image.
		 */
		public var imagePath:String = ImageHandler.ACTIONS + "transparent.png";

		/** The number of points the card is worth at the end of the game. */
		public var points:int;

		/** The list of SpecialPoint that define how the card behaves at the end of the game. */
		public var specialPoints:ArrayCollection;

		public var resourceZone:String;

		public var familyType:String;

		public var numberOfPlayers:int;
		
		/** Seems necessary for data binding to not have null resources. */
		public function get resources():HashCollection
		{
			if (this._resources == null)
			{
				this._resources = new HashCollection();
			}
			return this._resources;
		}
		
		public function set resources(res:HashCollection):void
		{
			this._resources = res;
		}

		public function addResource(type:String, quantity:int):void
		{
			if (resources == null)
			{
				resources = new HashCollection();
			}
			if (resources.getValue(type) != null)
			{
				quantity += resources.getValue(type);
			}
			resources.put(type, quantity);
		}

		public function resetResources():void
		{
			if (this.resources != null)
			{
				for each (var type:String in resources.getKeys())
				{
					this.resources.put(type, 0);
				}
			}
		}

		public function addPeep(peep:Peep):void
		{
			if (this.peeps == null)
			{
				this.peeps = new ArrayCollection();
			}
			this.peeps.addItem(peep);
		}

		public function addSpecialPoint(specialPoint:SpecialPoint):void
		{
			if (this.specialPoints == null)
			{
				this.specialPoints = new ArrayCollection();
			}
			this.specialPoints.addItem(specialPoint);
		}

		public function toString():String
		{
			return this.cardName;
		}

	}
}